package com.orange.cetback.controller.admin;

import com.orange.cetback.common.vo.Result;
import com.orange.cetback.pojo.entity.Admin;
import com.orange.cetback.pojo.vo.AdminLoginVO;
import com.orange.cetback.pojo.vo.AdminVO;
import com.orange.cetback.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 管理员信息表 前端控制器
 * </p>
 *
 * @author chenenjun
 * @since 2025-03-09
 */
@Tag(name = "管理员信息表接口")
@Slf4j
@RestController
@RequestMapping("/admin-api/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @Operation(summary = "管理员登录")
    @PostMapping("/login")
    public Result<AdminLoginVO> login(@RequestBody Admin admin){
        log.info("管理员登录：{}",admin);
        String token = adminService.login(admin);
        if (token == null){
            return Result.fail("用户名或密码错误");
        }
        return Result.success(new AdminLoginVO(token));
    }

    @Operation(summary = "获取管理员信息")
    @GetMapping("/info")
    public Result<AdminVO> info(@RequestParam String token){
        Admin admin = adminService.getAdmin(token);
        AdminVO adminVO = new AdminVO();
        BeanUtils.copyProperties(admin,adminVO);
        adminVO.setPassword(null);
        List<String> roles = new ArrayList<>();
        roles.add("admin");
        adminVO.setRoles(roles);
        return Result.success(adminVO);
    }

    @Operation(summary = "退出")
    @GetMapping("/logout")
    public Result<String> logout(){
        return Result.success("退出成功");
    }
}
